package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.AddNursingPlanDto;
import com.zzyl.dto.NursingPlanSearchDTO;
import com.zzyl.dto.UpdateNursingPlanDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.entity.NursingProjectPlan;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.mapper.NursingProjectPlanMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingPlanDetailVo;
import com.zzyl.vo.NursingProjectPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class NursingPlanServiceImpl implements NursingPlanService {

    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    @Autowired
    private NursingProjectPlanMapper projectPlanMapper;

    @Override
    public PageResponse<NursingPlan> search(NursingPlanSearchDTO dto) {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        Page<NursingPlan> page = nursingPlanMapper.search(dto);
        return PageResponse.of(page, NursingPlan.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(AddNursingPlanDto dto) {
        NursingPlan np = BeanUtil.copyProperties(dto, NursingPlan.class);
        nursingPlanMapper.insert(np);
        List<AddNursingPlanDto.Plan> projectPlans = dto.getProjectPlans();
        if(!CollectionUtils.isEmpty(projectPlans)){
            List<NursingProjectPlan> collect = projectPlans.stream().map(pp -> {
                NursingProjectPlan npp = BeanUtil.copyProperties(pp, NursingProjectPlan.class);
                npp.setPlanId(np.getId());
                return npp;
            }).collect(Collectors.toList());
            projectPlanMapper.insertBatch(collect);
        }
    }

    @Override
    public NursingPlanDetailVo getDetails(Long id) {
        NursingPlan np = nursingPlanMapper.selectById(id);
        if(np == null){
            throw new BaseException(BasicEnum.DATA_NOT_EXIST);
        }
        List<NursingProjectPlanVo> projectPlanList = projectPlanMapper.selectByPlanId(id);
        NursingPlanDetailVo vo = BeanUtil.copyProperties(np, NursingPlanDetailVo.class);
        vo.setProjectPlans(projectPlanList);
        return vo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long id) {
        nursingPlanMapper.deleteById(id);
        projectPlanMapper.deleteByPlanId(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(UpdateNursingPlanDto dto) {
        NursingPlan np = BeanUtil.copyProperties(dto, NursingPlan.class);
        // 修改plan
        nursingPlanMapper.update(np);
        // 删掉旧的关系
        projectPlanMapper.deleteByPlanId(dto.getId());
        // 插入新的关系
        if(!CollectionUtils.isEmpty(dto.getProjectPlans())){
            List<NursingProjectPlan> collect = dto.getProjectPlans().stream().map(p -> {
                NursingProjectPlan plan = BeanUtil.copyProperties(p, NursingProjectPlan.class);
                plan.setPlanId(dto.getId());
                return plan;
            }).collect(Collectors.toList());
            projectPlanMapper.insertBatch(collect);
        }
    }
}